package com.atguigu.registration.cmn.controller;

import com.atguigu.registration.cmn.service.DictService;
import com.atguigu.registration.common.result.Result;
import com.atguigu.registration.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: Jiang Yizhi
 * @Date: 2022/4/16 9:21 AM
 */
@Api(value = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;


    @ApiOperation(value = "根据id查询子数据列表")
    @GetMapping("getChildData/{id}")
    public Result getChildData(@PathVariable Long id) {
        List<Dict> list = dictService.getChildData(id);
        return Result.ok(list);
    }

    @GetMapping("exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportDictData(response);
    }

    @ApiOperation(value = "导入")
    @PostMapping("importData")
    public Result importData(MultipartFile file) {
        dictService.importData(file);
        return Result.ok();
    }

    /** 根据dictCode和value查询 */
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        String dictName = dictService.getDictName(dictCode, value);
        return dictName;
    }

    /** 根据value查询 */
    @GetMapping("getName/{value}")
    public String getName(@PathVariable String value) {
        String dictName = dictService.getDictName("", value);
        return dictName;
    }


    @ApiOperation(value = "根据dictCode获取下级节点 " )
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode) {
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

}

